#pragma once
#include "handlers/request_handler.h"
#include "handlers/hello_handler.h"
#include "handlers/not_found_handler.h"
#include <map>
#include <string>

namespace http_dispacher {
    class DispatcherServlet {
    private:
        std::map<std::string, http_dispacher::handlers::RequestHandler*> handlers;

    public:
        DispatcherServlet() {
            handlers["/hello"] = new http_dispacher::handlers::HelloHandler();
            handlers["/"] = new http_dispacher::handlers::NotFoundHandler();
        }

        ~DispatcherServlet() {
            for (auto& handler : handlers) {
                delete handler.second;
            }
        }

        void dispatch(struct evhttp_request* req) {
            const char* uri = evhttp_request_get_uri(req);
            std::string path(uri);

            auto it = handlers.find(path);
            if (it != handlers.end()) {
                it->second->handleRequest(req);
            }
            else {
                handlers["/"]->handleRequest(req);
            }
        }
    };
}